#!/bin/bash

# Auto-SSL 诊断脚本
# 帮助用户快速诊断 auto-ssl 命令问题

echo "🔍 Auto-SSL 系统诊断"
echo "===================="
echo ""

# 1. 检查auto-ssl命令
echo "1. 📋 检查 auto-ssl 命令状态:"
if command -v auto-ssl >/dev/null 2>&1; then
    auto_ssl_path=$(which auto-ssl)
    echo "   ✅ 命令路径: $auto_ssl_path"
    
    if [ -f "$auto_ssl_path" ]; then
        echo "   ✅ 文件存在"
        
        # 显示文件前几行
        echo "   📄 文件内容预览:"
        head -n 5 "$auto_ssl_path" 2>/dev/null | sed 's/^/      /'
        
        # 检查文件是否指向正确的脚本
        if grep -q "auto_ssl_universal.py" "$auto_ssl_path" 2>/dev/null; then
            echo "   ✅ 指向正确的脚本文件"
        elif grep -q "auto_ssl_setup.py" "$auto_ssl_path" 2>/dev/null; then
            echo "   ❌ 指向旧的脚本文件 (auto_ssl_setup.py)"
        else
            echo "   ⚠️  无法确定指向的脚本文件"
        fi
    else
        echo "   ❌ 文件不存在: $auto_ssl_path"
    fi
else
    echo "   ❌ 未找到 auto-ssl 命令"
fi

echo ""

# 2. 检查相关文件
echo "2. 📁 检查相关文件状态:"
files=(
    "/usr/local/bin/auto_ssl_universal.py:新版本主脚本"
    "/usr/local/bin/auto-ssl:命令行工具"
    "/root/auto_ssl_setup.py:旧版本脚本"
    "/usr/local/bin/auto-ssl-renew.sh:续期脚本"
)

for item in "${files[@]}"; do
    file="${item%%:*}"
    desc="${item##*:}"
    
    if [ -f "$file" ]; then
        echo "   ✅ $desc: $file"
    else
        echo "   ❌ $desc: $file (不存在)"
    fi
done

echo ""

# 3. 检查Python环境
echo "3. 🐍 检查 Python 环境:"
if command -v python3 >/dev/null 2>&1; then
    python_version=$(python3 --version 2>&1)
    echo "   ✅ Python3: $python_version"
else
    echo "   ❌ Python3 未安装"
fi

echo ""

# 4. 检查依赖工具
echo "4. 🔧 检查依赖工具:"
tools=("certbot" "nginx" "curl" "wget")

for tool in "${tools[@]}"; do
    if command -v "$tool" >/dev/null 2>&1; then
        echo "   ✅ $tool: 已安装"
    else
        echo "   ❌ $tool: 未安装"
    fi
done

echo ""

# 5. 尝试执行测试
echo "5. 🧪 命令测试:"
if command -v auto-ssl >/dev/null 2>&1; then
    echo "   测试 auto-ssl --version:"
    if auto-ssl --version 2>/dev/null; then
        echo "   ✅ 命令执行成功"
    else
        echo "   ❌ 命令执行失败"
        echo "   错误信息:"
        auto-ssl --version 2>&1 | sed 's/^/      /'
    fi
else
    echo "   ❌ 无法测试，auto-ssl 命令不存在"
fi

echo ""

# 6. 提供解决方案
echo "🔧 解决方案建议:"
echo ""

if ! command -v auto-ssl >/dev/null 2>&1; then
    echo "   问题: auto-ssl 命令不存在"
    echo "   解决: curl -sSL https://gitee.com/tght1211/auto-ssl/raw/master/install.sh | sudo bash"
elif [ -f "/usr/local/bin/auto-ssl" ] && grep -q "auto_ssl_setup.py" "/usr/local/bin/auto-ssl" 2>/dev/null; then
    echo "   问题: auto-ssl 指向旧版本脚本"
    echo "   解决: curl -sSL https://gitee.com/tght1211/auto-ssl/raw/master/fix-auto-ssl.sh | sudo bash"
elif [ ! -f "/usr/local/bin/auto_ssl_universal.py" ]; then
    echo "   问题: 缺少主脚本文件"
    echo "   解决: curl -sSL https://gitee.com/tght1211/auto-ssl/raw/master/install.sh | sudo bash"
else
    echo "   ✅ 系统状态正常，如仍有问题请检查权限"
    echo "   临时方案: python3 /usr/local/bin/auto_ssl_universal.py --help"
fi

echo ""
echo "📞 获取支持:"
echo "   - 项目地址: https://gitee.com/tght1211/auto-ssl"
echo "   - 问题反馈: 请提供此诊断结果"
